package klass.parse.util;

import java.io.IOException;
import java.io.InputStream;

import static klass.parse.util.ByteUtils.*;

public class InputStreamUtils {
    public static byte[] next(InputStream inputStream, int length) {
        byte[] bytes = new byte[length];
        try {
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Parse error!");
        }
        return bytes;
    }

    public static String nextString(InputStream inputStream, int length) {
        return new String(next(inputStream, length));
    }

    public static byte nextByte(InputStream inputStream) {
        return next(inputStream, 1)[0];
    }

    public static short nextShort(InputStream inputStream) {
        return byte2Short(next(inputStream, 2));
    }

    public static int nextInt(InputStream inputStream) {
        return byte2Integer(next(inputStream, 4));
    }

    public static long nextLong(InputStream inputStream) {
        return byte2Long(next(inputStream, 8));
    }

    public static double nextDouble(InputStream inputStream) {
        return byte2Double(next(inputStream, 8));
    }
}
